﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Wicresoft.Common;

public partial class Page_AttendenceStatDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) BindData();
    }
    protected void lbtnExport_Click(object sender, EventArgs e)
    {
        BindData();
        DataTable dt = BindData();
        string filename = "值班明细统计";
        if (txtStartDate.Text != string.Empty)
        {
            filename += Convert.ToDateTime(txtStartDate.Text).ToString("(yyyy年MM月dd日");
        }
        else
        {
            filename += "(             ";
        }
        if (txtEndDate.Text != string.Empty)
        {
            filename += "-" + Convert.ToDateTime(txtEndDate.Text).ToString("yyyy年MM月dd日)");
        }
        else
        {
            filename += "-             )";
        }
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(filename) + ".xls");
        Response.ContentType = "application/ms-excel";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.Write("<table>");
        string tableHeader = @"<tr class=""Normal"">
                                    <td width=""120"" align=""center"" rowspan=""2"">
                                        序号
                                    </td>
                                    <td width=""120"" align=""center"" rowspan=""2"">
                                        姓名
                                    </td>
                                    <td rowspan=""2"" width=""120"">
                                        日期
                                    </td>
                                    <td rowspan=""2"" width=""120"">
                                        时间
                                    </td>
                                    <td colspan=""2"" align=""center"">
                                        值班类型
                                    </td>
                                </tr>";
        Response.Write(tableHeader);
        int i = 1;
        foreach (DataRow dr in dt.Rows)
        {
            Response.Write(string.Format("<tr><td>{0}</td><td>{1}</td><td>{2:yyyy/MM/dd}</td><td>{3}</td><td>{4}</td></tr>",
                                         i,
                                         dr["UserDisplayName"],
                                         dr["AttendDate"],
                                         dr["AttendPriod"],
                                         dr["AttendType"]));
            i += 1;
        }
        Response.Write("</table>");
        Response.End();
    }
    protected void lbtnSearch_Click(object sender, EventArgs e)
    {
        DataTable dt = BindData();

    }
    private DataTable BindData()
    {
        string where = "1=1";
        if (!string.IsNullOrEmpty(txtStartDate.Text))
            where += string.Format(" AND AttendDate >= '{0} 00:00:00'", txtStartDate.Text);
        if (!string.IsNullOrEmpty(txtEndDate.Text))
            where += string.Format(" AND AttendDate <= '{0} 23:59:59'", txtEndDate.Text);
        string sql = string.Format(
                      @"select UserName,UserDisplayName,AttendDate,attendTimeStart,attendTimeEnd,isWeekEnd,isHoliday
                        from View_Attend_All
                        where {0}
                        order by AttendDate DESC,UserDisplayName", where);
        DataTable dt = DataAccess.ExecuteSql("Attendences.ConnectionString", sql).Tables[0];
        if (dt != null)
        {
            dt.Columns.Add("AttendType", typeof(string));
            dt.Columns.Add("AttendPriod", typeof(string));
            foreach (DataRow dr in dt.Rows)
            {
                string typeName = "";
                if ((bool)dr["isWeekEnd"] || (bool)dr["isHoliday"])
                    typeName = "节假日";
                DateTime start = DateTime.Parse(dr["attendTimeStart"].ToString());
                DateTime end = DateTime.Parse(dr["attendTimeEnd"].ToString());
                if (start.Hour >= 8 && end.Hour <= 18) typeName += "白天";
                else typeName += "晚上";
                dr["AttendType"] = typeName;
                dr["AttendPriod"] = start.ToString("HH:mm") + "-" + end.ToString("HH:mm");
            }
        }
        rptInfo.DataSource = dt;
        rptInfo.DataBind();
        return dt;
    }
}